package warehouse;

/**
 *
 * @author Manolescu Razvan
 */
import java.sql.ResultSet;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.JFreeChart;


public final class View extends JFrame {

    public ResultSet result;
    public JFreeChart chart;
    public int chart_type;
    public String chart_title;
    public String xlabel;
    public String ylabel;
    public String query;
    public DefaultTableModel dataModel;
    // 1-placinta
    // 2-XY
    // 3-bar

    public View(final ResultSet result, int chart_type, String chart_title, String xlabel, String ylabel, String query) throws Exception {
        super("Datamart view");

        this.result = result;
        //this.setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1200, 600);
        //setPreferredSize(new Dimesion());
        setResizable(false);
        this.chart_type = chart_type;
        this.chart_title = chart_title;
        this.xlabel = xlabel;
        this.ylabel = ylabel;
        this.query = query;
        drawChart(chart_type);
        result.beforeFirst();


        int colCount = 2;
        String[] colNames = new String[colCount];
        colNames[0] = xlabel;
        colNames[1] = ylabel;
        dataModel = new DefaultTableModel(colNames, 0);//setColumnIdentifiers( colNames );


        while (result.next()) {
            String[] rowData = new String[colCount];
            for (int i = 1; i <= colCount; i++) {
                rowData[i - 1] = result.getString(i);
            }
            dataModel.addRow(rowData);
        }
        result.beforeFirst();


    }

    public void run() throws Exception {
        drawChart(this.chart_type);
    }

    public void drawChart(int chart_type) throws Exception {
        switch (chart_type) {
            case 1:
                ChartXY c1 = new ChartXY();
                int k = 0;
                double[][] value1;
                result.beforeFirst();
                int cnum = result.getMetaData().getColumnCount();
                result.last();
                int size = result.getRow();
                result.beforeFirst();
                value1 = new double[cnum][size];

                while (result.next()) {
                    for (int p = 0; p < cnum; p++) {
                        value1[p][k] = new Double(result.getDouble(p + 1));
                    }
                    k++;
                    if (k == 1000) {
                        break;
                    }
                }
                c1.addXYDataSet(value1, chart_title);
                c1.createXYChart(chart_title, xlabel, ylabel);
                chart = c1.chart;
                break;
            case 2:
                Chart3DPie c2 = new Chart3DPie();
                int j = 0;
                String[] nume2 = new String[3000];
                double[] value2;
                value2 = new double[3000];
                while (result.next()) {
                    nume2[j] = result.getString(1);
                    value2[j++] = new Double(result.getDouble(2));
                }
                c2.createPieDataSet(nume2, value2);
                c2.create3DPieChart(this.chart_title);
                chart = c2.chart;
                break;
            case 3:

                ChartBars c3 = new ChartBars();
                int i = 0;
                String[] nume3 = new String[3000];
                double[] value3;
                value3 = new double[3000];
                while (result.next()) {
                    nume3[i] = result.getString(1);
                    value3[i++] = new Double(result.getDouble(2));
                }
                c3.addBarsDataSet(nume3, value3);
                c3.createBarsChart(this.chart_title, this.xlabel, this.ylabel);
                chart = c3.chart;
                break;
        }


        //chart.draw((Graphics2D) gr, (Rectangle2D) new Rectangle(0, 0, 500, 500));

//       String[] nume={"Adriana","Razvan"};
//       double[] value={10.0,10.0};
//       String titlu="Me and raz";
//       Chart3DPie c1 = new Chart3DPie();
//       c1.createPieDataSet(nume, value);
//       c1.create3DPieChart(titlu);
//       String fileLocation = "file.jpg";    //C:\\disertatie\\Warehouse\\   
//       Chart.saveChart(c1.getChart(), fileLocation);
//       
//       ChartXY c = new ChartXY();
//       double[][] data = { {0.1, 0.2, 0.3}, {1, 2, 3} };
//       c.addXYDataSet(data,"LALLAA");
//       double[][] data2 = { {0.1, 0.5, 0.1}, {1, 3, 2} };
//       c.addXYDataSet(data2,"a2a");
//       c.createXYChart("test", "x", "y");
//       fileLocation = "file3.jpg";
//       Chart.saveChart(c.chart, fileLocation);
//       String[] nume2={"Adriana","Razvan","lala"};
//       double[] value2={400.0,100.0,10.0};
//       ChartBars c3 = new ChartBars();
//       c3.addBarsDataSet(nume2, value2);
//       c3.createBarsChart("test", "x", "y");
//        fileLocation = "file4.jpg";
//       Chart.saveChart(c3.chart, fileLocation);   
    }
}